﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FSS.Core.Controls;
using IBatisNet.Common.Logging;
using Ext.Net;
using FSS.BLL.Entities;
using FSS.BLL.Services;
using FSS.Core.Util;

namespace SalesManagement.Modules.List
{
    public partial class Unit : BasePage, IBaseListPage
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(Country));
        public Unit()
            : base(SCREENS.UNIT)
        {

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            base.CheckPermissionControl();
            this.WindowEditor.SaveClick += new EventHandler(WindowEditor_SaveClick);
        }

        void WindowEditor_SaveClick(object sender, EventArgs e)
        {
            BindingGrid();
        }

        [DirectMethod]
        public void DocumentReady()
        {
            BindingGrid();
        }
        private void BindingGrid()
        {
            UnitEntity unitE = new UnitEntity();
            UnitService unitS = UnitService.GetInstance();
            List<UnitEntity> listUnit = (List<UnitEntity>)unitS.LoadAll();
            if (!listUnit.Equals(null))
            {
                storeUnit.DataSource = listUnit;
                storeUnit.DataBind();
            }
        }
        protected void grdMain_OnCommand(object sender, DirectEventArgs e)
        {
            int id = Int32.Parse(e.ExtraParams["id"]);
            string command = e.ExtraParams["command"];
            if (IsEdit && Constants.Edit.Equals(command))
            {
                this.WindowEditor.Show(id);
            }
            else if (IsDelete && Constants.Delete.Equals(command))
            {
                UnitEntity unitE = new UnitEntity();
                UnitService unitS = UnitService.GetInstance();
                unitE.UnitId = id;
                unitS.Delete(unitE);
                BindingGrid();
            }
        }
        protected void grdMain_OnAfterEdit(object sender, DirectEventArgs e)
        {
            try
            {
                bool IsActive = bool.Parse(e.ExtraParams["IsActive"]);
                int id = int.Parse(e.ExtraParams["UnitId"]);
                UnitEntity unitE = new UnitEntity();
                UnitService unitS = UnitService.GetInstance();
                unitE.IsActive = IsActive;
                unitE.UnitId = id;
                unitS.UnitUpdateCheck(unitE);
            }
            catch (Exception ex)
            {
                X.Msg.Alert("Error", ex.Message).Show();
            }
        }
        public void btnAdd_Click(object sender, DirectEventArgs e)
        {
            this.WindowEditor.ShowAndReset();
        }
        public void btnEdit_Click(object sender, DirectEventArgs e) { }
        public void btnDelete_Click(object sender, DirectEventArgs e) { }
        public void btnPrint_Click(object sender, DirectEventArgs e) { }
        public void btnImport_Click(object sender, DirectEventArgs e) { }
        public void btnExport_Click(object sender, DirectEventArgs e) { }
        public void btnFind_Click(object sender, DirectEventArgs e) { }
    }
}
